{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "E_dmhy5jdm8B"
   },
   "source": [
    "由于第一部分我们聚焦在 python 的基础知识的学习上，主要是让大家的属性 python 语法知识上，且能方便记录学习的过程，所以搭建环境我选择最简单的方式。\n",
    "对于后面在项目实践的时候，会搭建适合项目开发的 python 开发环境。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "c83JNgAEAFFb"
   },
   "source": [
    "这里使用 docker 方式搭建我们的学习环境，我们选择 `jupyter/base-notebook` 镜像，它同时兼顾了学习练习和笔记记录的功能，并且可以将学习比价导出，对于我们前期学习 python3 基础知识是最合适的方式，这里对于容器的管理我们使用 `docker-compose`，对于后面有更多需求的时候，也可以很方便的进行扩展或替换成其他版本。当然你也可以选择你喜欢的 docker 镜像版本：[jupyter/docker-stack](https://hub.docker.com/u/jupyter/)。这系列文章也是使用一个在线的类似 Jupyter 工具 [Colaboratory](https://colab.research.google.com/) 写的（后面会介绍）。\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "qXY9Q58vVebv"
   },
   "source": [
    "# Docker 安装\n",
    "\n",
    "*已经有 docker 环境的用户跳过*\n",
    "\n",
    "## docker 安装\n",
    "\n",
    "docker 安装可以查看[docker 安装](https://blog.threeq.me/post/articles/noob-learn-sql/1-install-tools/#docker-%E5%AE%89%E8%A3%85)\n",
    "\n",
    "## docker-compose 安装\n",
    "\n",
    "请参考官方文档：[https://docs.docker.com/compose/install/](https://docs.docker.com/compose/install/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "CNxhWbJqVkqt"
   },
   "source": [
    "# 本地 Jupyter\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "RmcwZ_8GVy-H"
   },
   "source": [
    "## Dockerfile"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 0,
   "metadata": {
    "colab": {
     "autoexec": {
      "startup": false,
      "wait_interval": 0
     }
    },
    "colab_type": "code",
    "id": "LbeOlKBzfZzn"
   },
   "outputs": [],
   "source": [
    "FROM jupyter/base-notebook\n",
    "MAINTAINER threeq<threeq@foxmail.com>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "YFp-IYJsGtef"
   },
   "source": [
    "## docker-compose.yml"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 0,
   "metadata": {
    "colab": {
     "autoexec": {
      "startup": false,
      "wait_interval": 0
     }
    },
    "colab_type": "code",
    "id": "uK6WiWDeG3Uz"
   },
   "outputs": [],
   "source": [
    "version: '3'\n",
    "\n",
    "services:\n",
    "  notebook:\n",
    "    build: .\n",
    "    ports:\n",
    "      - 8888:8888\n",
    "    volumes:\n",
    "      - ./work:/home/jovyan"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "xgj28LhiLmW0"
   },
   "source": [
    "将这两个文件放到同一个目录，使用 `docker-compose up -d` 启动 jupyter 服务器，然后使用 `docker-compose logs` 查看启动是否成功和登录使用的 **token**。看到日日志输入类似\n",
    "\n",
    "```\n",
    "Attaching to nooblearnpython_notebook_1\n",
    "notebook_1  | /usr/local/bin/start-notebook.sh: ignoring /usr/local/bin/start-notebook.d/*\n",
    "notebook_1  |\n",
    "notebook_1  | Container must be run with group \"root\" to update passwd file\n",
    "notebook_1  | Executing the command: jupyter notebook\n",
    "notebook_1  | [W 07:23:14.610 NotebookApp] WARNING: The notebook server is listening on all IP addresses and not using encryption. This is not recommended.\n",
    "notebook_1  | [I 07:23:14.686 NotebookApp] JupyterLab beta preview extension loaded from /opt/conda/lib/python3.6/site-packages/jupyterlab\n",
    "notebook_1  | [I 07:23:14.686 NotebookApp] JupyterLab application directory is /opt/conda/share/jupyter/lab\n",
    "notebook_1  | [I 07:23:14.693 NotebookApp] Serving notebooks from local directory: /home/jovyan\n",
    "notebook_1  | [I 07:23:14.694 NotebookApp] 0 active kernels\n",
    "notebook_1  | [I 07:23:14.694 NotebookApp] The Jupyter Notebook is running at:\n",
    "notebook_1  | [I 07:23:14.694 NotebookApp] http://f5a65ccd5cb8:8888/?token=ed3a63c025a0f44ba00a8cb4e39f28cadebc4d8f6679c603\n",
    "notebook_1  | [I 07:23:14.695 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).\n",
    "notebook_1  | [C 07:23:14.703 NotebookApp]\n",
    "notebook_1  |\n",
    "notebook_1  |     Copy/paste this URL into your browser when you connect for the first time,\n",
    "notebook_1  |     to login with a token:\n",
    "notebook_1  |         http://f5a65ccd5cb8:8888/?token=ed3a63c025a0f44ba00a8cb4e39f28cadebc4d8f6679c603&token=ed3a63c025a0f44ba00a8cb4e39f28cadebc4d8f6679c603\n",
    "```\n",
    "\n",
    "最后一行就是就是访问的地址和 token，打开本地浏览器输入地址：[http://localhost:8888](http://localhost:8888)，会看到登录界面输入最后一行的 **token**，就能进入 jupyter 列表界面\n",
    "\n",
    "![jupyter 笔记列表](https://)\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "Lv_1heNQvn7b"
   },
   "source": [
    "## 测试环境正确性\n",
    "\n",
    "开始我们第一个 python 代码，也是经典的学习入门程序：Hello，World！\n",
    "\n",
    "> 新建一个学习笔记，直接在代码块中输入以下代码，按 `Shift+Enter` 执行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "colab": {
     "autoexec": {
      "startup": false,
      "wait_interval": 0
     },
     "base_uri": "https://localhost:8080/",
     "height": 34
    },
    "colab_type": "code",
    "executionInfo": {
     "elapsed": 1288,
     "status": "ok",
     "timestamp": 1528970349564,
     "user": {
      "displayName": "wang san",
      "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s128",
      "userId": "110912998189487813609"
     },
     "user_tz": -480
    },
    "id": "mKY-WR9KuzO-",
    "outputId": "a932f23c-565d-4e89-ac48-3c3321515b94"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello, World!\n"
     ]
    }
   ],
   "source": [
    "print(\"Hello, World!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "H_87aBYBBdSv"
   },
   "source": [
    "# 在线 Jupyter\n",
    "\n",
    " 对于可以访问 Google 服务的小伙伴，还可以使用 Google 的一个在线服务：[Colaboratory](https://colab.research.google.com/)，Colaboratory 本身是一个数据分析工具，但是这个完全满足我们对于 Python3 学习使用。访问地址：https://colab.research.google.com\n",
    "\n",
    "这个可以使我们更快速的学习实践，同时将我们的学习笔记存储 google 网盘，只是国内用户访问 google 服务需要使用科学方法。"
   ]
  }
 ],
 "metadata": {
  "colab": {
   "collapsed_sections": [],
   "default_view": {},
   "name": "01-learn-environment-install.ipynb",
   "provenance": [],
   "version": "0.3.2",
   "views": {}
  },
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
